package sicnu.cs.ich.activity.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import sicnu.cs.ich.activity.domain.ActivityLike;
import sicnu.cs.ich.activity.domain.CommentLike;
import sicnu.cs.ich.activity.mapper.ActivityCommentMapper;
import sicnu.cs.ich.activity.service.IActivityLikeService;
import sicnu.cs.ich.activity.service.ICommentLikeService;
import sicnu.cs.ich.activity.service.ICommentService;
import sicnu.cs.ich.activity.util.SensitiveWordUtil;
import sicnu.cs.ich.api.domain.entity.ActivityComments;
import sicnu.cs.ich.api.domain.vo.CommentVO;

import java.util.*;

/**
 * @author 黎琦
 * @date 2021/12/8 15:44
 **/
@Service
@RequiredArgsConstructor
public class CommentServiceImpl implements ICommentService {
    private final ActivityCommentMapper activityCommentMapper;
    private final ICommentLikeService commentLikeService;
    private final IActivityLikeService activityLikeService;

    @Override
    public List<CommentVO> getCommentByAid(Integer aid) {
        //查询所有评论
        List<CommentVO> commentVOList = activityCommentMapper.getCommentsByAid(aid);
        if(commentVOList==null){
            throw new NoSuchElementException("该活动没有评论");
        }
        //用于根据cid来查找评论内容
        HashMap<Integer, CommentVO> map = new HashMap<>(commentVOList.size());
        Map<Integer, CommentLike> commentLikes = commentLikeService.getCommentLikesByAid(aid);

        for (CommentVO commentVO : commentVOList) {

            commentVO.setLikeNum(commentLikes.get(commentVO.getId()).getLikeNum());
            map.put(commentVO.getId(), commentVO);
        }
        //初始化返回的第一层评论
        ArrayList<CommentVO> commentVOFather = new ArrayList<>();
        for (CommentVO commentVO : commentVOList) {
            if(commentVO.getParentId()==0){
                //是父级评论
                commentVOFather.add(commentVO);
            }else{
                //是子评论
                CommentVO parent = map.get(commentVO.getParentId());
                commentVO.setParentName(parent.getNickname());
                //将该子评论插入第一层评论的回复列表中
                CommentVO ancestor = map.get(commentVO.getAncestorId());
                List<CommentVO> replayList = ancestor.getReplayList();
                if(replayList==null){
                    replayList = new ArrayList<>();
                }
                replayList.add(commentVO);
            }
        }
        return commentVOList;
    }

    @Override
    public Integer insertComment(ActivityComments activityComments) {
        //替换敏感词汇
        activityComments.setContent(SensitiveWordUtil.filterInfo(activityComments.getContent()));
        //插入评论主表
        activityComments.setTime(new Date());
        activityComments.setLikeNum(0);
        activityComments.setStatus(1);

        //父评论
        ActivityComments father;
        if(activityComments.getParentId()==0){
            //为第一层评论，其祖先id=0
            activityComments.setAncestorId(0);
        }else if((father=activityCommentMapper.selectById(activityComments.getParentId())).getParentId()==0){
            //如果父评论的父id为0，说明是第二层评论，其祖先id=其父id
            activityComments.setAncestorId(activityComments.getParentId());
        }else{
            //其他层
            activityComments.setAncestorId(father.getAncestorId());
        }
        activityCommentMapper.insert(activityComments);

        //初始化评论点赞表
        commentLikeService.insert(CommentLike.builder()
                .aid(activityComments.getActivityId())
                .cid(activityComments.getId())
                .likeNum(0L)
                .build());

        //更新活动评论数
        activityLikeService.updateCommentNumByAid(activityComments.getActivityId());
        return activityComments.getId();
    }

    @Override
    public Boolean deleteComment(Integer cid) {
        return activityCommentMapper.updateStatusById(cid, -1)>0;
    }
}
